package grame

import org.junit.Test

/**
 * @author Shining Wang <shiningwang@topca.com>
 */
class AppVersion implements Comparable<AppVersion>{
    String value;

    AppVersion(String value) {
        this.value = value
    }

    String toString(){
        return value;
    }


    @Override
    int compareTo(AppVersion appVersion) {
        int[] v1Parts = value.findAll(/\d+/){it as int}
        int[] v2Parts = appVersion.value.findAll(/\d+/){it as int}
        v1Parts.eachWithIndex {v,i-> i<v2Parts.size() && (v1Parts[i] = v2Parts[i] = (v1Parts[i]-v2Parts[i]) ) }
        return v1Parts.find{it} ?: (v2Parts.find{it}?:0)
    }
}

class AppVersionTest{
    @Test
    void testAppVersion(){
        println "Testing AppVersion..."
        assert new AppVersion("2.5.0") < new AppVersion("2.5.1.2")
        assert new AppVersion("2.5.0.5.2") < new AppVersion("2.5.1.2")
        assert new AppVersion("2.5.10") > new AppVersion("2.5.1.2")
        assert new AppVersion("2.5.0") == new AppVersion("2.5")
    }
}
